package com.woniuxy.commons.core.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {

    private static final String adminSecret = "askdfjLKASJDFLASJFaslkdfjalksjdf13123lksdfjlkasfasdfasdfadf";

    private static final String userSecret = "askdfjLKASJDFLaksdfdksaslkdfjalksjdf131123iwuerdfjlkasfasdfasdfadf";

    /**
     * 生成Token
     * @param body
     * @param time 秒
     * @return
     */
    public static String createToken(Map<String, Object> body,Integer time,String secret) {
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
        JwtBuilder jb = Jwts.builder();
        if(body == null || body.size() <= 0) throw new RuntimeException("没有数据");
        jb.setClaims(body);
        if(time == null) jb.setExpiration(new Date(System.currentTimeMillis() + 60 * 300 * 1000));
        else jb.setExpiration(new Date(System.currentTimeMillis() + time * 1000));
        return jb.signWith(key).compact();
    }

    /**
     * 生成Token
     * @param body
     * @return
     */
    public static String createToken(Map<String, Object> body,String secret) {
        return createToken(body,null,secret);
    }

    /**
     * 解析
     * @param token
     * @return
     */
    public static Claims parseToken(String token,String secret) throws Exception {
        if(token == null) throw new RuntimeException("token为空");
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }

    /**
     * 生成User端Token
     * @param body
     * @return
     */
    public static String createUserToken(Map<String, Object> body) {
        return createToken(body,userSecret);
    }


    /**
     * 生成User端Token
     * @param body
     * @return
     */
    public static String createUserToken(Object... body) {
        if(body == null || body.length <= 0) throw new RuntimeException("没有主体数据");
        Map<String, Object> maps = new HashMap<>();
        for(int i=0;i<body.length;i+=2) {
            Object value = null;
            if(i+1 < body.length) value = body[i+1];
            maps.put(body[i].toString(), value);
        }
        return createToken(maps,userSecret);
    }

    /**
     * 生成Admin端Token
     * @param body
     * @return
     */
    public static String createAdminToken(Map<String, Object> body) {
        return createToken(body,adminSecret);
    }

    /**
     * 生成Admin端Token
     * @param body
     * @return
     */
    public static String createAdminToken(Object... body) {
        if(body == null || body.length <= 0) throw new RuntimeException("没有主体数据");
        Map<String, Object> maps = new HashMap<>();
        for(int i=0;i<body.length;i+=2) {
            Object value = null;
            if(i+1 < body.length) value = body[i+1];
            maps.put(body[i].toString(), value);
        }
        return createToken(maps,adminSecret);
    }

    /**
     * 解析User端token
     * @param token
     * @return
     */
    public static Claims parseUserToken(String token) throws Exception {
        return parseToken(token,userSecret);
    }

    /**
     * 解析Admin端token
     * @param token
     * @return
     */
    public static Claims parseAdminToken(String token) throws Exception {
        return parseToken(token,adminSecret);
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {

        System.out.println(createUserToken("userId",1,"role","admin"));
    }

}







